package com.yoomiito.app.adapter.home.reommend;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.bumptech.glide.Glide;
import com.yoomiito.app.R;
import com.yoomiito.app.base.App;
import com.yoomiito.app.listener.ItemClickListener;
import com.yoomiito.app.listener.OnBannerItemClickListener;
import com.yoomiito.app.model.bean.BannerAndBgInfo;
import com.yoomiito.app.model.bean.BannerAndIconInfo;
import com.yoomiito.app.widget.helper.IndicatorLocation;
import com.yoomiito.app.widget.helper.LoopLayout;
import com.yoomiito.app.widget.helper.LoopStyle;
import com.yoomiito.app.widget.helper.OnDefaultImageViewLoader;
import com.yoomiito.app.widget.view.BannerBgContainer;

import java.util.ArrayList;

/**
 * 首页---活动
 */
public class HomeBannerAdapter extends DelegateAdapter.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private LayoutHelper mLayoutHelper;
    private BannerAndBgInfo bannerAndBgInfo;
    private ItemClickListener mItemClickListener;

    public HomeBannerAdapter(Context context, LayoutHelper layoutHelper, BannerAndBgInfo bannerAndBgInfo) {
        mContext = context;
        mLayoutHelper = layoutHelper;
        this.bannerAndBgInfo = bannerAndBgInfo;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.home_item_banner,parent,false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
        itemViewHolder.mLoopLayout.setLoopData(bannerAndBgInfo.getDatas());
        itemViewHolder.mBgContainer.setBannerBackBg(mContext, bannerAndBgInfo.getBgUrls());
        itemViewHolder.mLoopLayout.startLoop();
    }

    @Override
    public int getItemCount() {
        return bannerAndBgInfo==null?0:1;
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    public void setNewData(BannerAndBgInfo bannerAndBgInfo) {
        this.bannerAndBgInfo = bannerAndBgInfo == null?new BannerAndBgInfo():bannerAndBgInfo;
        notifyDataSetChanged();
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    private class ItemViewHolder extends RecyclerView.ViewHolder {

        public LoopLayout mLoopLayout;
        public BannerBgContainer mBgContainer;
        public ItemViewHolder(View itemView) {
            super(itemView);
            mBgContainer = itemView.findViewById(R.id.banner_bg_container);
            mLoopLayout = itemView.findViewById(R.id.loop_layout);
            FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mLoopLayout.getLayoutParams();
            lp.height = (int) (App.mWidthPx*0.345);
            initBanner();
        }

        private void initBanner() {
            mLoopLayout.setLoop_ms(4000);//轮播的速度(毫秒)
            mLoopLayout.setLoop_duration(500);//滑动的速率(毫秒)
            mLoopLayout.setScaleAnimation(false);// 设置是否需要动画
            mLoopLayout.setLoop_style(LoopStyle.Zoom);//轮播的样式-默认empty
            mLoopLayout.setIndicatorLocation(IndicatorLocation.Center);//指示器位置-中Center
            mLoopLayout.initializeData(mContext);

            mLoopLayout.setOnLoadImageViewListener(new OnDefaultImageViewLoader() {
                @Override
                public void onLoadImageView(ImageView view, Object object) {
                    Glide.with(view.getContext())
                            .load(object)
                            .into(view);
                }
            });

            mLoopLayout.setOnBannerItemClickListener(new OnBannerItemClickListener() {
                @Override
                public void onBannerClick(int index, ArrayList<BannerAndIconInfo> banner) {
                    if (mItemClickListener!=null){
                        mItemClickListener.onItemClick(index);
                    }
                }
            });

            mLoopLayout.setBannerBgContainer(mBgContainer);
        }
    }
}
